import { Component, OnInit, Input } from '@angular/core';
import { Assembly, AssemblyStyles, AssemblyValidators } from '../../../fb-design.model'
import { FormGroup } from '@angular/forms';

@Component({
    selector: 'qk-dropdown',
    template: `
    <ng-container [formGroup]="form">
        <p-dropdown [formControlName]="assembly.id" [style]="assembly.attributes.style" [options]="options" [filter]="options && options.length > 10"></p-dropdown>
        <div *ngIf="formErrors[assembly.id]" class="alert alert-danger">
            <i class="fa fa-times"></i>{{formErrors[assembly.id]}}
        </div>
    </ng-container>
    `,
    styles: [`
        .alert-danger{
            background-color:#f8b7bd;
            margin-top:1px;
            padding:0.429em;
            color:#212121;
            z-index:999;
            position:relative;
        }
    `],
    providers: []
})
export class QkDropdown implements OnInit {
    @Input() assembly: Assembly;
    @Input() form: FormGroup;
    @Input() formErrors: any = {};
    @Input() mode: number;
    options: any[] = [{ 'label': '是', 'value': '1' }, { 'label': '否', 'value': '0' }]
    constructor() { }

    ngOnInit() {
        // this.addNullIfNotRequired();
        if(this.mode == 2){
            if(1 == this.assembly.authCode){//只读
                $("#mask_"+this.assembly.id).css({'z-index': 1, 'opacity': 0.5, 'background-color':'lightgray'});
            }else if(0 == this.assembly.authCode){//没有权限
                $("#mask_"+this.assembly.id).css({'z-index': 1, 'background-color':'lightgray'});
            }
        }
        if (this.assembly.configs && this.assembly.configs.items) {
            this.options = JSON.parse(this.assembly.configs.items) || this.options;
        } else {
            this.assembly.configs.items = JSON.stringify(this.options);
        }
    }

    ngDoCheck() {
        if(this.mode == 1){
            if (this.assembly.configs && this.assembly.configs.items) {
                this.options = JSON.parse(this.assembly.configs.items) || this.options;
            } else {
                this.assembly.configs.items = JSON.stringify(this.options);
            }
        }
        this.assembly.attributes.refreshStyle();
    }

    // private addNullIfNotRequired() {
    //     if (this.assembly.validators && !this.assembly.validators.required) {
    //         this.options.unshift({ 'label': '', 'value': '' });
    //     }
    // }

}
